<?php
/**
 * xineForum Engine
 * 
 * @author RiderSx <alexridersx@gmail.com>
 * @version 0.01
 */

defined('DIR') or define('DIR', dirname(__FILE__));

/**
 * Базовый класс
 * 
 * 
 * @author RiderSx
 *
 */
class xine {
	private static $_debug = false;
	
	public static $r = array(), $config, $cache, $ACS, $user, $model, $view, $controller;
	
	public static function getVersion () {
		return 0.16;
	}
	
	public static function getConfig ($string) {
		$path = DIR.'/cfg/'.$string.'.php';
		return include $path;
	}
	
	public static function factory ($needle) {
		$dir = strstr($needle, ':', true);
		
		if(!$dir) $dir = 'core';
		
		if(strstr($needle, '.')) {
			$needle = str_replace('.', DIRECTORY_SEPARATOR, $needle);
			$class = end(explode(DIRECTORY_SEPARATOR, $needle));
		} else {
			$class =& $needle;
		}
		
		$file = DIR.'/components/'.$dir.'/'.$needle.'.php';
		
		if(file_exists($file)) {
			include $file;
			return new $class;
		} else {
			return false;
		}
	}
	
	public static function runSubAction ($object, $subAction, $subActions=array()) {
		if(in_array($subAction, $subActions)) {
			$object->$subAction();
			return true;
		} else {
			return false;
		}
	}
}

/**
 * Основной класс приложения
 * 
 * @author RiderSx
 *
 */
class xineForum {
	
	/**
	 * Запуск и вывод приложения
	 * 
	 * @return unknown_type
	 */
	public static function RunForum () {
		ob_start();
		
		xine::$config = xine::getConfig('config');
		self::executeClasses();
		
		// Loading MVC structure
		self::loadGlobalMVC();
		self::loadMVC();
		
		// Loading Cache
		if(xine::$config['cacheEnabled'])
			xine::$cache = xine::factory('cache.xineCache')->getDriver(xine::$config['cache']['driver']);
		
		// Loading Access Control System
		$storage = xine::factory('ACS.xineUsersStorage');
		xine::$ACS = xine::factory('ACS.xineUsers');
		
		xine::$ACS->__init($storage->getStorage(xine::$config['storage']['driver']), xine::$cache, true);
		
		//Calling needed action
		if(xine::$r['components']['Router']->type == 'default') {
			$method = 'proceed'.strtoupper(xine::$controller->__default);
		} else {
			$method = 'proceed'.strtoupper(xine::$r['components']['Router']->type);
		}
		xine::$controller->$method();
		
		echo ob_get_clean();
	}
	
	/**
	 * Запуск классов необхожимых для работы приложения
	 * 
	 * @return null
	 */
	public static function executeClasses () {
		// Execute Event Dispatcher
		xine::$r['components']['EventDispatcher'] = xine::factory('xineEventDispatcher');
		
		// Execute Router with $offset parameter
		xine::$r['components']['Router'] = xine::factory('xineForumRouter');
	}
	
	/**
	 * Загрузка и инициализация MVC
	 * 
	 * @return null
	 */
	private static function loadMVC () {
		$action =& xine::$r['components']['Router']->action;
		
		if(file_exists(DIR.'/components/system/xineForumActions/'.$action.'_Action.php')) {
			$m=$action.'_Model';$v=$action.'_View';$c=$action.'_Controller';
			
			//Model Calling
			if(file_exists(DIR.'/components/system/xineForumModels/'.xine::$config['storage']['driver'].'/'.$action.'_Model.php')) {
				include DIR.'/components/system/xineForumModels/'.xine::$config['storage']['driver'].'/'.$action.'_Model.php';
				xine::$model = new $m;
			}
			
			//View Calling
			if(file_exists(DIR.'/components/system/xineForumViews/'.$action.'_View.php')) {
				include(DIR.'/components/system/xineForumViews/'.$action.'_View.php');
				xine::$view = new $v;
			}
			
			//Controller Calling
			if(file_exists(DIR.'/components/system/xineForumActions/'.$action.'_Action.php')) {
				include DIR.'/components/system/xineForumActions/'.$action.'_Action.php';
				xine::$controller = new $c;
			}
		} else {
			include DIR.'/components/system/xineForumViews/cnf_View.php';
			xine::$view = new cnf_View;
			include DIR.'/components/system/xineForumActions/cnf_Action.php'; // Content Not Found
			xine::$controller = new cnf_Controller;
		}
	}
	
	/**
	 * Загрузка глобальной структуры MVC
	 * 
	 * 
	 */
	public static function loadGlobalMVC () {
		include DIR.'/components/system/xineForumModels/'.xine::$config['storage']['driver'].'/global.Model.php';
		include DIR.'/components/system/xineForumViews/global.View.php';
		include DIR.'/components/system/xineForumActions/global.Action.php';
	}
	
	/**
	 * Загрузка и настройка плагинов
	 * 
	 * @return unknown_type
	 */
	/*private static function loadPlugins () {
		$active = include DIR.'/cfg/activePlugins.php';
		foreach($active as $plugin) {
			if(file_exists(DIR.'/components/plugins/'.$plugin.'/'.$plugin.'.php')) {
				include DIR.'/components/plugins/'.$plugin.'/'.$plugin.'.php';
				xine::$r['plugins'][$plugin] = new $plugin;
				xine::$r['plugins'][$plugin]->__init();
			}
		}
	}*/
}

function xineAutoload ($className) {
	$path = DIR.'/components/classes/'.$className.'.php';
	
	if(file_exists($path)) xine::factory($className);
	else return false;
}
spl_autoload_register('xineAutoload');